package servlets;

import java.io.IOException;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import models.QuestFinal;

/**
 * Servlet implementation class QuestionnaireFinal
 */

@WebServlet(name="QuestionnaireFinal", urlPatterns={"/JSP/Maha/QuestionnaireFinal"})
public class QuestionnaireFinal extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public QuestionnaireFinal() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String titre=request.getParameter("titre");
		
		int nbrQL=Integer.parseInt((String)request.getParameter("nbrQL"));

		int nbrQM=Integer.parseInt((String)request.getParameter("nbrQM"));

		int nbrQU=Integer.parseInt((String)request.getParameter("nbrQU"));
		int total=nbrQL+nbrQM+nbrQU;
	
		
//get l'ordre des questions		
		TreeMap tm=new TreeMap();
		for(int i=1;i<=total;i++){
			if(request.getParameter(i+"QL")!=null)	{tm.put(i+"QL",request.getParameter(i+"QL"));}
			else	if(request.getParameter(i+"QM")!=null)	{tm.put(i+"QM",request.getParameter(i+"QM"));}
			else    if(request.getParameter(i+"QU")!=null)	{tm.put(i+"QU",request.getParameter(i+"QU"));}

		}
		
		

		
//get Question libre

		HashMap QL=new HashMap();
		for(int i=0;i<nbrQL;i++){
			QL.put(i,request.getParameter("ql"+i));
			

		}
//get Question multiple
		
		Hashtable   QMLibelle=new Hashtable();

		Hashtable   QMPos=new Hashtable();


		for(int j=0;j<nbrQM;j++){

			String sj=j+"";
		
			QMLibelle.put(sj,request.getParameter("qm"+j));
			//nbre de possibilites
			int     nbrpos=0;
			nbrpos=Integer.valueOf((String)request.getParameter("pos"+j)).intValue(); 
			

			for (int t=0;t<nbrpos;t++)
			{
				String posM="posM";
				posM+=j;
				posM+=t;
				String st=j+""+t; 
				QMPos.put(st,request.getParameter(posM));

			}

		

		}

	//get Question unique
		
		
				Hashtable   QULibelle=new Hashtable();

				Hashtable   QUPos=new Hashtable();


				for(int j=0;j<nbrQU;j++){

					String sj=j+"";
					QULibelle.put(sj,request.getParameter("qu"+j));
					//nbre de possibilites
					int     nbrpos=0;
					nbrpos=Integer.valueOf((String)request.getParameter("posbU"+j)).intValue(); 
					

					for (int t=0;t<nbrpos;t++)
					{
						String posU="posU";
						posU+=j;
						posU+=t;
						String st=j+""+t; 
						QUPos.put(st,request.getParameter(posU));

					}

				

				}


		QuestFinal ce = new QuestFinal();

		ce.getTitle(titre);
		ce.getNbrQM(nbrQM);
		ce.getNbrQU(nbrQU);
		ce.getLibre(QL);
		ce.getQMLibelle(QMLibelle);
		ce.getQMPos(QMPos);
		ce.getQULibelle(QULibelle);
		ce.getQUPos(QUPos);
		ce.getTM(tm);
		
		request.setAttribute("titre", titre);
		request.setAttribute("nbrQL",nbrQL);
		request.setAttribute("QL", QL);
		request.setAttribute("QMLibelle", QMLibelle);
		request.setAttribute("nbrQM",nbrQM);
		request.setAttribute("QMPos", QMPos);

		request.setAttribute("QULibelle", QULibelle);
		request.setAttribute("nbrQU",nbrQU);
		request.setAttribute("QUPos", QUPos);
		request.setAttribute("TM", tm);
		RequestDispatcher view = request.getRequestDispatcher("QuestionnaireFinal.jsp");

		view.forward(request, response); 		
	}

}
